package com.im.util;

import java.util.Map;
import java.util.Set;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.im.MDA;
import com.im.controller.UserService;

import sun.misc.BASE64Decoder;

public class AESEncryptUtil {

	private static final Logger logger = LogManager.getLogger(AESEncryptUtil.class);

	private static final String KEY = "abcdefgabcdefg12";

	public static String Encrypt(String sSrc) throws Exception {

		String sKey = "1234567896543210";// manager.getString("CONST_AES_PAGE_KEY");
		// sKey="1234567896543210";
		if (sKey == null) {
			logger.info("Key为空null");
			return null;
		}
		// 判断Key是否为16位
		if (sKey.length() != 16) {
			logger.info("Key长度不是16位");
			return null;
		}
		byte[] raw = sKey.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
		IvParameterSpec iv = new IvParameterSpec(MDA.AES_IV.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
		byte[] sSrcBytes = sSrc.getBytes("UTF-8");
		byte[] encrypted = cipher.doFinal(sSrcBytes);

		return Base64.encodeBase64String(encrypted);// 此处使用BAES64做转码功能，同时能起到2次加密的作用。
	}

	// 解密
	public static String Decrypt(String sSrc) throws Exception {
		String sKey = "1234567896543210";
		try {
			// 判断Key是否正确
			if (sKey == null) {
				logger.info("Key为空null");
				return null;
			}
			// 判断Key是否为16位
			if (sKey.length() != 16) {
				logger.info("Key长度不是16位");
				return null;
			}
			byte[] raw = sKey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(MDA.AES_IV.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			byte[] encrypted1 = Base64.decodeBase64(sSrc);// 先用bAES64解密
			try {
				byte[] original = cipher.doFinal(encrypted1);
				String originalString = new String(original, "UTF-8");
				return originalString;
			} catch (Exception e) {
				logger.error(e.getMessage());
				return null;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		// ConstManager manager = ConstManagerFactory.getConstManagerImpl();
		// Map map = manager.getConstMap();
		// Set keys = map.keySet();
		// for (Iterator iter = keys.iterator(); iter.hasNext();) {
		// String key = (String) iter.next();
		// System.out.println("var " + key + "= '" + map.get(key) + "';\n");
		// }

		String str = "{statusId:2000,itemClassId:100000}";
		try {
			logger.info(AESEncryptUtil.Encrypt(str));
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		try {
			String objString = "TapJx0Tg6PANvCaNiss+rw==";
			logger.info(AESEncryptUtil.Decrypt(objString));
		} catch (Exception e) {
			logger.error(e.getMessage());			
		}
	}
}
